package com.example.lib_mvp.base;


import android.app.Service;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.view.View;

import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.example.lib_mvp.musicService.MusicService;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;
import com.tjf.lib_core.BaseFragment;
import com.tjf.lib_core.upload.TipDialog;
import com.tjf.lib_utils.EventBusUtils;
import com.tjf.lib_utils.LogsUtils;
import com.tjf.lib_utils.bean.BusEvent;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

public abstract class AppBaseFragment extends BaseFragment<AppBaseActivity> {


/*    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        LogsUtils.i("fragment==", "onCreate 方法");

    }*/


    /**
     * 需要接收事件 重写该方法 并返回true
     *
     * @return boolean
     */
    protected boolean registerEventBus() {
        return true;
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventBus(BusEvent event) {

    }

    @Override
    public void onStart() {
        super.onStart();
        if (registerEventBus()) {
            LogsUtils.i("fragment==", " EventBusUtils.register 方法");
            EventBusUtils.register(this);
        }
    }

    @Override
    public void onStop() {
        super.onStop();
        if (registerEventBus()) {
            LogsUtils.i("fragment==", " EventBusUtils.unregister 方法");
            EventBusUtils.unregister(this);
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        LogsUtils.i("fragment==", "onDestroyView 方法");
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        LogsUtils.i("fragment==", "onDestroy 方法");
    }


    public void tipSendDialog(String title, String msg, View.OnClickListener listener) {
        TipDialog.Builder builder = new TipDialog.Builder(getActivity(), title, msg);
        builder.show();
        builder.getTv_ok().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                builder.dismiss();
                listener.onClick(v);
            }
        });
    }


    public void setSmartRefresh(SmartRefreshLayout smartRefreshLayout,
                                OnRefreshLoadMoreListener listener) {
        getAttachActivity().setSmartRefresh(smartRefreshLayout, listener);
    }

    public void setSmartRefreshHeader(SmartRefreshLayout smartRefreshLayout,
                                      OnRefreshListener listener) {

        getAttachActivity().setSmartRefreshHeader(smartRefreshLayout, listener);
    }

    public void setEmptyView(BaseQuickAdapter adapter, RecyclerView recyclerView) {

        getAttachActivity().setEmptyView(adapter, recyclerView);
    }


    public MusicService.MusicBinder musicBinder;
    public MusicServiceConnection musicServiceConnection;
    boolean isBind = false;
    private static final int BIND_AUTO_CREATE = Service.BIND_AUTO_CREATE;

    public void startAndBindService() {
        LogsUtils.i("startAndBindService==启动音乐服务=");

        Intent intent = new Intent(getActivity(), MusicService.class);
        musicServiceConnection = new MusicServiceConnection();
        getActivity().startService(intent);
        getActivity().bindService(intent, musicServiceConnection, BIND_AUTO_CREATE);
    }

    public void stopAndBindService() {
        if (musicBinder != null) {
            musicBinder.stopPlay();
        }
        Intent intent = new Intent(getActivity(), MusicService.class);
        if (musicServiceConnection != null) {
            getActivity().unbindService(musicServiceConnection);
        }
        getActivity().stopService(intent);
    }

    class MusicServiceConnection implements ServiceConnection {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            musicBinder = (MusicService.MusicBinder) service;
            isBind = true;
            LogsUtils.i("onServiceConnected=数据==绑定音乐服务成功=");

        }

        @Override
        public void onServiceDisconnected(ComponentName name) {

        }
    }


}
